package cn.com.fstar.demo.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.com.fstar.framework.base.controller.BaseController;
import cn.com.fstar.framework.base.model.EasyUIComboTree;
import cn.com.fstar.framework.base.model.EasyUIDataGrid;
import cn.com.fstar.framework.base.model.JsonResult;
import cn.com.fstar.core.CodeConstant;
import cn.com.fstar.core.model.TcontentAttachment;
import cn.com.fstar.core.pageModel.Content;
import cn.com.fstar.core.pageModel.Json;
import cn.com.fstar.core.pageModel.PageHelper;
import cn.com.fstar.core.pageModel.UploadFileModel;
import cn.com.fstar.core.util.FileDownloadUtil;
import cn.com.fstar.core.util.FileUpload;
import cn.com.fstar.core.util.LogUtil;
import cn.com.fstar.demo.service.AttachmentService;
import cn.com.fstar.demo.service.ContentService;

/**
 * 内容管理
 * 
 * @author 
 */

@Controller
@RequestMapping("/content")
public class DemoContentController extends BaseController {

    /** 日志类 */
    private static final Logger LOG = Logger.getLogger(DemoContentController.class);
    @Autowired
    private ContentService contentService;
    @Autowired
    private AttachmentService attachmentService;

    /**
     * 跳转到图表页面
     * 
     * @return
     */
    @RequestMapping("/showList")
    public String forwardPageList() {
        return "/demo/attachment/contentList";
    }

    /**
     * 跳转到编辑页面
     * 
     * @return
     */
    @RequestMapping("/editPage")
    public String forwardPageEdit(HttpServletRequest request) {
        String id = request.getParameter("id");
        Content demoContent = contentService.getById(id);
        request.setAttribute("content", demoContent);
        return "/demo/attachment/contentEdit";
    }

    /**
     * 添加页面
     * 
     * @param request
     * @return
     */
    @RequestMapping("/addPage")
    public String forwardPageAdd(HttpServletRequest request) {
        return "/demo/attachment/contentAdd";
    }

    @ResponseBody
    @RequestMapping("/add")
    public JsonResult add(Content demoContent, HttpServletRequest request) {

        JsonResult jsonResult = new JsonResult();
        if (demoContent == null) {
            jsonResult.setMsg("表单为空！");
            jsonResult.setObj("error");
            jsonResult.setSuccess(false);
        }

        try {
            String msg = contentService.add(demoContent);
            jsonResult.setObj(msg);
            jsonResult.setSuccess(true);
            jsonResult.setMsg("成功！");
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return jsonResult;
    }

    @RequestMapping("/contentDetail")
    public String contentDetail(String id, HttpServletRequest request) {
        Content democontent = contentService.getById(id);
        request.setAttribute("data", democontent);
        return "/demo/attachment/contentDetail";

    }

    /**
     * 删除
     * 
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(String id, HttpServletRequest request) {

        JsonResult json = new JsonResult();
        try {
            contentService.delete(id);
            json.setSuccess(true);
            json.setMsg("删除成功！");

        } catch (Exception e) {
            LOG.error(e.getMessage());
            json.setSuccess(false);
            json.setMsg("删除失败！");
        }
        return json;
    }

    /**
     * 修改内容
     */
    @ResponseBody
    @RequestMapping("/edit")
    public JsonResult editDemoContent(Content demoContent, HttpServletRequest request) {

        // 定义easyUI Json对象
        JsonResult j = new JsonResult();
        // 调用Service方法
        String msg = contentService.edit(demoContent);
        if (msg == "success") {
            j.setSuccess(true);
            j.setObj("success");
            j.setMsg("内容修改成功！");
            LogUtil.adminLogs("内容修改成功：", CodeConstant.EDIT_OPERATION, request);
        } else {
            j.setObj("error");
            j.setSuccess(false);
            j.setMsg("未知错误，请联系系统维护人员！");
            LOG.error("未知错误，请联系系统维护人员");
        }

        return j;
    }

    /**
     * 获得内容列表
     * 
     * @return
     */
    @RequestMapping("/dataGrid")
    @ResponseBody
    public EasyUIDataGrid dataGrid(PageHelper ph, HttpServletRequest request) {
        return contentService.dataGrid(ph);
    }

    @ResponseBody
    @RequestMapping("/isOutLinkComboTree")
    public List<EasyUIComboTree> outLinkComboTree() {
        return contentService.outLinkComboTree();
    }

    @ResponseBody
    @RequestMapping("/statusComboTree")
    public List<EasyUIComboTree> statusComboTree() {
        return contentService.statusComboTree();
    }

    /**
     * 
     * 处理多文件上传
     * 
     * @param response
     * @param request
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/uploadFiles")
    public UploadFileModel uploadFiles(HttpServletResponse response, HttpServletRequest request) throws IOException {

        UploadFileModel uploadFile = FileUpload.uploadFile((MultipartHttpServletRequest) request);

        return uploadFile;
    }

    /**
     * PDF文件下载
     * 
     * @param request
     * @param response
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/download")
    public String download(HttpServletRequest request, HttpServletResponse response, String id) throws Exception {

        TcontentAttachment attachment = attachmentService.get(id);
        FileDownloadUtil.downloadCanonicalFile(request, response, attachment.getLink(), attachment.getName());
        return null;
    }

    /**
     * 
     * 描述: 根据附件ID删除附件
     *
     * @param id
     * @return
     * @throws Exception
     *             Json
     */
    @RequestMapping("/deleteAttach")
    @ResponseBody
    public Json deleteAttachById(String id) throws Exception {
        Json j = new Json();
        try {
            attachmentService.deleteAttachById(id);
            j.setMsg("删除成功！");
            j.setSuccess(true);
        } catch (Exception e) {
            j.setMsg("删除失败！");
        }
        return j;
    }
}
